#include "exporter.h"
#include "csvexporter.h"

Exporter::Exporter(QObject *parent) : QObject(parent)
{
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Exporter::doWork);
}

Exporter::~Exporter()
{
    stop();
    timer->deleteLater();
}

void Exporter::start()
{
    if (!timer->isActive()) {
        timer->start(intval);
        qDebug() << "Worker started.";
    }
}

void Exporter::stop()
{
    if (timer->isActive()) {
        timer->stop();
        qDebug() << "Worker stopped.";
    }
}

void Exporter::path(QString p, int t)
{
    filePath = p;
    intval = t;
//    qDebug() << filePath;
}

void Exporter::GiveMeList(QVector<GraphsData *> list)
{
    ExportList = list;
//    qDebug() << ExportList;
}

void Exporter::doWork()
{
    qDebug() << "Working...";
    CsvExporter ex;
    QStringList list;
    list.append(QTime::currentTime().toString());
    for(int i = 0; i < ExportList.size(); i++)
        list.append(QString::number(*ExportList.at(i)->value * (*ExportList.at(i)->MultipNum)));
    ex.addRow(list);
    if(!ex.exportToFile(filePath))
    {
        stop();
        emit(TaskFail());
    }
//    emit taskDone("Task completed at " + QTime::currentTime().toString());
}
